'Imports Microsoft.DirectX.AudioVideoPlayback
Imports System.Threading
Imports Microsoft.DirectX.DirectSound

Namespace Sound

    Module mDirectSound

        Private Class MirSound

            Private mFile As String
            Private mSound As SecondaryBuffer
            Private mTag As String
            Private mIsOriginal As Boolean

            Public ReadOnly Property Tag() As String
                Get
                    Return mTag
                End Get
            End Property

            Public ReadOnly Property Buffer() As Microsoft.DirectX.DirectSound.SecondaryBuffer
                Get
                    Return mSound
                End Get
            End Property

            Public ReadOnly Property SoundFile() As String
                Get
                    Return mFile
                End Get
            End Property

            Public ReadOnly Property IsOriginal() As Boolean
                Get
                    Return mIsOriginal
                End Get
            End Property

            Public Sub New(ByVal file As String, ByVal vol As Int32, Optional ByVal tag As String = "")

                mTag = tag
                mFile = Globals.sSoundPath & file

                If Not FileIO.FileSystem.FileExists(mFile) Then Exit Sub

                If tag = "BGM" Then
                    vol = BGMVolume
                ElseIf vol = -1 Then
                    vol = EffectsVolume
                Else
                    '////----original code
                    If vol > 100 Then vol = 100
                    If vol < 0 Then vol = 0

                    vol = ((vol / 100) * 10000) - 10000
                    '----////
                End If
                
                mSound = Sound.CloneSound(file)
                If mSound Is Nothing Then
                    mSound = New SecondaryBuffer(mFile, DS)
                    mIsOriginal = True
                End If

                mSound.Volume = vol

                mSound.Play(0, BufferPlayFlags.Default)
                SoundList.Add(Me)

            End Sub

        End Class

        Public DS As Device
        Public BuffDesc As BufferDescription

        Private SoundList As New ArrayList()

        Public Sub Init()

            'SoundThread = New Thread(AddressOf SoundLoop)
            'SoundThread.Start()

            DS = New Device()
            DS.SetCooperativeLevel(Globals.fGameForm, CooperativeLevel.Normal)

            BuffDesc = New BufferDescription()
            BuffDesc.ControlEffects = False

            BGMVolume = Globals.iniConfig.GetInteger("SOUND", "BGMVOLUME", 75)
            EffectsVolume = Globals.iniConfig.GetInteger("SOUND", "EFFECTSVOLUME", 100)

        End Sub

        Public Sub DoSound()

            If SoundList.Count > 0 Then

                Dim currsound As MirSound

                For i As Integer = SoundList.Count - 1 To 0 Step -1

                    currsound = CType(SoundList(i), MirSound)

                    If Not currsound.Buffer.Status.Playing And Not currsound.IsOriginal Then
                        currsound.Buffer.Dispose()
                        SoundList.RemoveAt(i)
                    End If

                Next

            End If

        End Sub

        Public Function CloneSound(ByVal file As String) As SecondaryBuffer

            If SoundList.Count = 0 Then Return Nothing

            Dim currsound As MirSound

            For i As Integer = SoundList.Count - 1 To 0 Step -1

                currsound = CType(SoundList(i), MirSound)

                If currsound.SoundFile = file Then
                    Return currsound.Buffer
                End If

            Next

            Return Nothing

        End Function

        Public Sub Shutdown()

            'SoundThread.Abort()
            DS.Dispose()

        End Sub

        'Private Sub SoundLoop()

        'Dim i As Integer

        'Do Until Globals.bEnding

        'For i = 0 To SOUNDBUFFERCOUNT - 1
        'LoopSound(EffectSounds(i))
        'Next

        'LoopSound(BGMSound)

        'Application.DoEvents()
        'System.Threading.Thread.Sleep(100)

        'Loop

        'End Sub

        'Private Sub LoopSound(ByRef Sound As SoundStruct)
        'With Sound
        'If Not .buffer Is Nothing Then
        'If Not .buffer.Disposed Then
        'If .buffer.Duration = .buffer.CurrentPosition Then
        'If .looping Then
        'loop
        '.buffer.SeekCurrentPosition(0, SeekPositionFlags.AbsolutePositioning)
        'Else
        'end
        '.buffer.Stop()
        '.buffer.Dispose()
        '.buffer = Nothing
        'If Not .callback Is Nothing Then
        '.callback.Invoke(.tag)
        'End If
        'End If
        'End If
        'End If
        'End If
        'End With
        'End Sub

        Public Sub PlaySoundEffect(ByRef file As String, ByRef vol As Integer, Optional ByRef looping As Boolean = False, Optional ByRef callback As SoundCallbackDelegate = Nothing, Optional ByRef tag As String = "")

            Dim s As MirSound = New MirSound(file, vol, tag)

            'Dim disp As Boolean

            'For i As Integer = 0 To SOUNDBUFFERCOUNT - 1
            'With EffectSounds(i)

            'If Not .buffer Is Nothing Then
            'If .buffer.Disposed Then disp = True
            'End If

            'If .buffer Is Nothing Or disp Then
            '.tag = tag
            '.callback = callback
            '.looping = looping
            '.volume = vol
            '.buffer = New Audio(Globals.sSoundPath & file)
            '.buffer.Volume = (((vol \ 100) * EffectsVolume) - 100) * 100
            '.buffer.Play()
            'Exit Sub
            'End If

            'End With
            'Next

            'System.Threading.Thread.Sleep(50)
            'PlaySoundEffect(file, vol)

        End Sub

        Public Function GetSoundEffectFile(ByRef Sound As SoundEffects) As String

            Select Case Sound

                Case SoundEffects.Click
                    Return "103.wav"

                Case SoundEffects.ClickGo
                    Return "104.wav"

                Case SoundEffects.StartWalking
                    Return "1.wav"

                Case SoundEffects.Walking
                    Return "2.wav"

                Case SoundEffects.StopWalking
                    Return "3.wav"

                Case Else
                    Return ""

            End Select

        End Function

        Public Sub StopSoundEffect(ByRef tag As String)

            If SoundList.Count = 0 Then Exit Sub

            Dim currsound As MirSound

            For i As Integer = SoundList.Count - 1 To 0 Step -1

                currsound = CType(SoundList(i), MirSound)

                If Not currsound.Buffer Is Nothing Then
                    If currsound.tag = tag Then
                        currsound.buffer.Stop()
                        currsound.buffer.Dispose()
                    End If
                End If

            Next

        End Sub

        Public Sub PlayBGM(ByRef file As String, ByRef vol As Integer)

            StopBGM()
            Dim BGM = New MirSound(file, vol, "BGM")

        End Sub

        Public Function GetBGMFile(ByRef Sound As BGM) As String

            Select Case Sound

                Case BGM.Opening
                    Return "Opening.wav"

                Case BGM.SelectChar
                    Return "SelChr.wav"

                Case BGM.CreateChar
                    Return "CreateChr.wav"

                Case Else
                    Return ""

            End Select

        End Function

        Public Sub StopBGM()

            For Each s As MirSound In SoundList
                If s.Tag = "BGM" Then
                    If Not s.Buffer Is Nothing Then
                        If Not s.Buffer.Disposed Then
                            s.Buffer.Stop()
                        End If
                    End If
                End If
            Next

        End Sub

        Public Sub StopAllSounds()

            'For i As Integer = 0 To SOUNDBUFFERCOUNT - 1
            'With EffectSounds(i)
            'If Not .buffer Is Nothing Then
            'If Not .buffer.Disposed Then
            'Try
            '.buffer.Stop()
            '.buffer.Dispose()
            '.buffer = Nothing
            'Catch ex As Exception
            'End Try
            'End If
            'End If
            'End With
            'Next

            'StopBGM()

        End Sub

        Public Property EffectsVolume() As Integer

            Get
                Return volEffects
            End Get

            Set(ByVal val As Integer)
                If val > 100 Then val = 100
                If val < 0 Then val = 0

                Globals.iniConfig.WriteInteger("SOUND", "EFFECTSVOLUME", val)

                volEffects = ((val / 100) * 10000) - 10000
                DoEffectsVolume()
            End Set

        End Property

        Private Sub DoEffectsVolume()

            For Each sound As MirSound In SoundList

                If Not sound Is Nothing Then
                    If Not sound.Buffer Is Nothing Then
                        If Not sound.Buffer.Disposed Then
                            If sound.Tag <> "BGM" Then

                                sound.Buffer.Volume = EffectsVolume

                            End If
                        End If
                    End If
                End If

            Next

        End Sub

        Public Property BGMVolume() As Integer

            Get
                Return volBGM
            End Get

            Set(ByVal val As Integer)

                If val > 100 Then val = 100
                If val < 0 Then val = 0

                Globals.iniConfig.WriteInteger("SOUND", "BGMVOLUME", val)

                volBGM = ((val / 100) * 10000) - 10000
                'volBGM = ((val / 100) * 60) + 40

                For Each sound As MirSound In SoundList

                    If sound.Tag = "BGM" Then

                        sound.Buffer.Volume = volBGM

                    End If

                Next

                'If Not BGMSound.buffer Is Nothing Then
                'BGMSound.buffer.Volume = (((BGMSound.volume \ 100) * volBGM) - 100) * 100
                'End If

            End Set

        End Property

        Public Enum MobSounds
            Attack = 2
            Hit = 4
            Die = 5
        End Enum

        Public Sub PlayMobSound(ByVal mobID As Integer, ByVal snd As MobSounds)

            Dim file As String

            file = mobID + 200

            file &= "-" & snd & ".wav"

            PlaySoundEffect(file, 100)

        End Sub

    End Module

End Namespace
